package com.calvin.study.shiro;

import java.util.Collection;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.calvin.study.utils.ApplicationContextUtil;

public class RedisCache<K, V> implements Cache<K, V> {

	private String cacheName;

	public RedisCache() {
	}

	public RedisCache(String cacheName) {
		this.cacheName = cacheName;
	}

	private RedisTemplate getRedisTemplate() {
		RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtil.getBean("redisTemplate");
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		return redisTemplate;
	}

	@Override
	public void clear() throws CacheException {
		System.out.println("=============clear==============");
		getRedisTemplate().delete(this.cacheName);
	}

	@Override
	public V get(K k) throws CacheException {
		System.out.println("get key:" + k);
		return (V) getRedisTemplate().opsForHash().get(this.cacheName, k.toString());
	}

	@Override
	public Set<K> keys() {
		return getRedisTemplate().opsForHash().keys(this.cacheName);
	}

	@Override
	public V put(K k, V v) throws CacheException {
		System.out.println("put key: " + k);
		System.out.println("put value:" + v);
		getRedisTemplate().opsForHash().put(this.cacheName, k.toString(), v);
		return null;
	}

	@Override
	public V remove(K k) throws CacheException {
		System.out.println("=============remove=============");
		return (V) getRedisTemplate().opsForHash().delete(this.cacheName, k.toString());
	}

	@Override
	public int size() {
		return getRedisTemplate().opsForHash().size(this.cacheName).intValue();
	}

	@Override
	public Collection<V> values() {
		return getRedisTemplate().opsForHash().values(this.cacheName);
	}

}
